package com.ruoyi.web.controller.emp;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.EmpConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.EmpArea;
import com.ruoyi.system.domain.EmpNormCollect;
import com.ruoyi.system.domain.EmpNormSystem;
import com.ruoyi.system.service.IEmpAreaService;
import com.ruoyi.system.service.IEmpNormCollectService;
import com.ruoyi.system.service.IEmpNormSystemService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 指标采集Controller
 *
 * @author zsl
 * @date 2024-04-15
 */
@RestController
@RequestMapping("/system/collect")
@Slf4j
public class EmpNormCollectController extends BaseController {
    @Autowired
    private IEmpNormCollectService empNormCollectService;
    @Autowired
    private IEmpNormSystemService iEmpNormSystemService;
    @Autowired
    private IEmpAreaService iEmpAreaService;
    @Autowired
    private EmpConfig empConfig;

    /**
     * 查询指标采集列表
     */
    @PreAuthorize("@ss.hasPermi('system:collect:list')")
    @GetMapping("/list")
    public TableDataInfo list(EmpNormCollect empNormCollect) {
        startPage();
        List<EmpNormCollect> list = empNormCollectService.selectEmpNormCollectList(empNormCollect);
        return getDataTable(list);
    }

    /**
     * 导出指标采集列表
     */
    @PreAuthorize("@ss.hasPermi('system:collect:export')")
    @Log(title = "指标采集", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EmpNormCollect empNormCollect) {
        List<EmpNormCollect> list = empNormCollectService.selectEmpNormCollectList(empNormCollect);
        log.info("导出指标采集列表:{}", JSON.toJSONString(list));
        ExcelUtil<EmpNormCollect> util = new ExcelUtil<EmpNormCollect>(EmpNormCollect.class);
        util.exportExcel(response, list, "指标采集数据");
    }

    /**
     * 获取指标采集详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:collect:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(empNormCollectService.selectEmpNormCollectById(id));
    }

    /**
     * 新增指标采集
     */
    @PreAuthorize("@ss.hasPermi('system:collect:add')")
    @Log(title = "指标采集", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EmpNormCollect empNormCollect) {
        if (ObjectUtils.isEmpty(empNormCollect) || ObjectUtils.isEmpty(empNormCollect.getSystemId()) || ObjectUtils.isEmpty(empNormCollect.getAreaId())) {
            return toAjax(0);
        }
        empNormCollect.setCreateBy(getUsername());
        return toAjax(empNormCollectService.insertEmpNormCollect(empNormCollect));
    }

    /**
     * 修改指标采集
     */
    @PreAuthorize("@ss.hasPermi('system:collect:edit')")
    @Log(title = "指标采集", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EmpNormCollect empNormCollect) {
        empNormCollect.setUpdateBy(getUsername());
        return toAjax(empNormCollectService.updateEmpNormCollect(empNormCollect));
    }

    /**
     * 删除指标采集
     */
    @PreAuthorize("@ss.hasPermi('system:collect:remove')")
    @Log(title = "指标采集", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(empNormCollectService.deleteEmpNormCollectByIds(ids));
    }

    /**
     * 查询所有指标体系和查询行政区域
     */
    @PreAuthorize("@ss.hasPermi('system:collect:list')")
    @GetMapping(value = "/getSystem")
    public AjaxResult getSystem() {
        AjaxResult ajax = AjaxResult.success();
        //查询所有指标体系
        List<EmpNormSystem> empNormSystems = iEmpNormSystemService.selectEmpNormSystemList(new EmpNormSystem());
        //查询行政区域
        //List<EmpArea> empAreas = iEmpAreaService.selectEmpAreaList(new EmpArea());
        //开启分页
        int empAreaMaxSize = empConfig.getEmpAreaMaxSize();
        Page<EmpArea> page = new Page<>(1, empAreaMaxSize);
        LambdaQueryWrapper<EmpArea> wrapper = Wrappers.lambdaQuery(EmpArea.class).orderByAsc(EmpArea::getCode);
        List<EmpArea> records = iEmpAreaService.page(page, wrapper).getRecords();
        ajax.put("empNormSystems", empNormSystems);
        ajax.put("empAreas", records);
        return ajax;
    }

    /**
     * 导入指标采集
     */
    @Log(title = "指标采集", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:collect:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<EmpNormCollect> util = new ExcelUtil<>(EmpNormCollect.class);
        List<EmpNormCollect> list = util.importExcel(file.getInputStream());
        log.info("导入指标采集数量：{}", list.size());
        String operName = getUsername();
        String message = empNormCollectService.importEmpNormCollect(list, updateSupport, operName);
        return success(message);
    }

    /**
     * 下载模块指标采集
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<EmpNormCollect> util = new ExcelUtil<>(EmpNormCollect.class);
        util.importTemplateExcel(response, "指标采集数据");
    }
}
